from ultralytics import YOLO
import cv2
import time

model = YOLO("yolov8x.pt")

# Open the video file
video_path = "D:/pyourch-workspace/yoloy8-project/20240805_105902.mp4"
cap = cv2.VideoCapture(video_path)

if not cap.isOpened():
    print("打开摄视屏失败！")

# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()
    time.sleep(5)
    if success:
        print("视频读帧成功！")
        # Run YOLOv8 inference on the frame
        results = model(frame)

        # Visualize the results on the frame
        annotated_frame = results[0].plot()

        # Display the annotated frame
        cv2.imshow("YOLOv8 Inference", annotated_frame)

        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        print("视频读帧失败！")
        # Break the loop if the end of the video is reached
        break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()


